<!DOCTYPE html>
<html>

<head>

    <title>异步</title>

</head>

<body>


</body>


<script>
    function* fun(){
   console.log("start");
   var x = yield '2';
   console.log(x);
   var y = yield '3';
   console.log(y);
   console.log((x + y));
   }

   //没传参
   var s1 = fun();
   s1.next();
   s1.next();
   s1.next();

    //有参
    var s2 = fun();
    s2.next();
    s2.next(20);
    s2.next(30);


    // function* f(){
    //     yield 1;

    //     yield 2;
    // }

    // var a=f();
    // console.log(a.next());

    // console.log( a.return(3));

    // console.log( a.next());


    // function* foo() {
    //     yield "a";
    //     yield "b";
    // }


    // //var f=foo();
    // function* gen(x, y) {
    //     yield 1;
    //     yield 2;
    //     yield* foo();
    //     // for(var value of f){
    //     //     yield value;
    //     // }
    //     yield 3;
    // }
    // var g = gen();
    // console.log(g.next());
    // console.log(g.next());
    // console.log(g.next());
    // console.log(g.next());
    // console.log(g.next());

    //例子
    //准备
    /*function prepare(sucess) {
        setTimeout(function () {
            console.log("prepare chicken");
            sucess();
        }, 500)
    }

    //炒鸡
    function fired(sucess) {
        setTimeout(function () {
            console.log("fired chicken");
            sucess();
        }, 500)
    }
    //炖鸡
    function stewed(sucess) {
        setTimeout(function () {
            console.log("stewed chicken");
            sucess();
        }, 500)
    }
    //上料
    function sdd(sucess) {
        setTimeout(function () {
            console.log("sdd chicken");
            sucess();
        }, 500)
    }
    //上菜
    function serve(sucess) {
        setTimeout(function () {
            console.log("serve chicken");
            sucess();
        }, 500)
    }

    //流程控制
    function run(fn) {
        const gen = fn();
        function next() {
            //返回工序函数的句柄给result
            const result = gen.next();
            if (result.done) return;//结束
            // result.value就是yield返回的值，是各个工序的函数
            result.value(next);//next作为入参，即本工序成功后，执行下一工序
        }
        next();
    };
    //工序
    function* task() {
        yield prepare;
        yield fired;
        yield stewed;
        yield sdd;
        yield serve;
    }

    run(task);*/



</script>

</html>